package com.tdam2014.grupo01;


import java.util.List;

import com.tdam2014.grupo01.adapters.PhotoSetListAdapter;
import com.tdam2014.grupo01.interfaces.DatabaseUpdatedListener;

import java.util.Collections;
import java.util.List;

import com.tdam2014.grupo01.PhotoListActivity.TitleComparator;
import com.tdam2014.grupo01.adapters.PhotoListAdapter;
import com.tdam2014.grupo01.adapters.PhotoSetListAdapter;
import com.tdam2014.grupo01.interfaces.DatabaseUpdatedListener;
import com.tdam2014.grupo01.objects.DialogInfo;
import com.tdam2014.grupo01.objects.Photo;
import com.tdam2014.grupo01.objects.Photoset;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends ListActivity {
	public static final String DIRECTORY_NAME = "dirName";
	public static final String DIRECTORY_ID = "dirID";
	
	private DialogInfo dialogInfoToShow;
	private List<Photoset> lista;
	private DatabaseUpdatedListener listener;

	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
        final PhotoSetListAdapter adapter = new PhotoSetListAdapter(this);
		final DatabaseHelper db = new DatabaseHelper(this);
        
		loadDialogData();
		
		showProgressDialog();

		db.UpdatePhotosetDatabase();
		
		dialogInfoToShow.setShowing(true);
		
		lista = db.getAllPhotoSets();
		adapter.setList(lista);
		setListAdapter(adapter);
		
		listener = new DatabaseUpdatedListener() {
			@Override
			public void updateListAdapter() {
				List<Photoset> directorios = db.getAllPhotoSets();
				for(Photoset dir : directorios){
					if(!directorios.contains(dir)){
						directorios.add(dir);
					}
					else{
						Photoset direc = new Photoset();
						
						direc.setId(dir.getId());
						direc.setSecret(dir.getSecret());
						direc.setPrimary(dir.getPrimary());
						direc.setServer(dir.getServer());
						direc.setTitle(dir.getTitle());
						
						if(lista.lastIndexOf(dir) == -1)
						{
							lista.add(direc);
						}
						else
						{
							lista.set(lista.lastIndexOf(dir), direc);
						}
					}	
					//Collections.sort(lista, new TitleComparator());
					adapter.notifyDataSetChanged();	
				}
				hideProgressDialog();
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(MainActivity.this, R.string.listaDirectoriosActualizada, Toast.LENGTH_SHORT).show();
					}
				});
			}

			
			@Override
			public void notifyErrorUpdating(final String error) {
				hideProgressDialog();
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(MainActivity.this, R.string.errorDeConexion, Toast.LENGTH_LONG).show();
					}
				});
				
			}


			@Override
			public void notifyNoValues() {
				// TODO Auto-generated method stub
				
			}
		};
		
		db.setDatabaseUpdatedListener(listener);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Photoset photoSet = (Photoset) adapter.getItem(position);
				
				Intent intent = new Intent(MainActivity.this, PhotoListActivity.class);
				intent.putExtra(DIRECTORY_NAME, photoSet.getTitle());
				intent.putExtra(DIRECTORY_ID, photoSet.getId());
				startActivity(intent);
			}
		});
    }
    
    private void loadDialogData() {
		dialogInfoToShow = new DialogInfo();
		dialogInfoToShow.setTitle(R.string.actualizandoListaDirectorios);
		dialogInfoToShow.setType(DialogInfo.TYPE_PROGRESS);
		dialogInfoToShow.setMessage(R.string.actualizando);
	}
    
    private void showProgressDialog() {
		showDialog(DialogInfo.TYPE_PROGRESS);
	}
    
	   private Dialog createProgressDialog() {

			OnDismissListener onDimiss = new OnDismissListener() {

				@Override
				public void onDismiss(DialogInterface dialog) {
						dialogInfoToShow.setShowing(false);	
						removeDialog(DialogInfo.TYPE_PROGRESS);
				}
			};

			ProgressDialog progressDialog;
			progressDialog = new ProgressDialog(this);
			progressDialog.setOnDismissListener(onDimiss);
			progressDialog.setTitle(dialogInfoToShow.getTitle());
			progressDialog.setMessage(getResources().getString(R.string.actualizando));
			progressDialog.setCancelable(true);
			return progressDialog;
		}
	    
	    @Override
		protected Dialog onCreateDialog(int id) {
			Dialog dialog = null;
			if (id == DialogInfo.TYPE_PROGRESS) {
				dialog = createProgressDialog();
			} 
			return dialog;
		}
    
	private void hideProgressDialog() 
	{
		if (dialogInfoToShow.isShowing())
		{
			removeDialog(dialogInfoToShow.getType());
		}
	}
}

